/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: S.M.A.R.T. - Self-Monitoring, Analysis and Reporting Technology
* Version: 0.1
*
* License: creative commons BY-NC-SA
*/


var mod_sysmonitor_plugin_smart_refstring = "a";

// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

if(system.platform=="windows") {
myPlugins["smart"] = new Object();
myPlugins["smart"]["name"] = getLocalizedString2("mod_sysm_plugin_smart_name");
myPlugins["smart"]["onlyonce"] = false;
myPlugins["smart"]["newObject"] = function (NewSettings) { return new plugin_smart_sensor(NewSettings); }
}

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_smart_sensor(NewSettings) {
	// Required variables
	this.plugin       = "smart";
	this.settings     = NewSettings;
	this.edit         = plugin_Edit;
	this.ReDraw       = plugin_ReDraw;
	this.frame        = new Frame();
	this.frame.window = main;
	this.frame.width  = mod_sysmonitor_width;


	//Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.barcolor) this.settings.barcolor = "#008000";
	if(!this.settings.updspeed) this.settings.updspeed = 60;
	if(!this.settings.showUnit) this.settings.showUnit = 0;
	if(!this.settings.smartID) {
		var smartDevices = new Array();
		if(VBSEnabled) smartDevices = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_smart + '"').split("\n");
		else smartDevices = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "DeviceID" "1" "SELECT * FROM Win32_DiskDrive"').split("\n");

		if(smartDevices.length>0) {
			this.settings.smartID = smartDevices[0];
		} else {
			alert(getLocalizedString2("mod_sysm_plugin_smart_no_devices_found"));
			this.canceledplugin = true;
			return false;
		}
	}
	if(!this.settings.smartLabel) this.settings.smartLabel = "";
	if(!this.settings.show_mixed) this.settings.show_mixed = 0;
	if(!this.settings.show_194) this.settings.show_194 = 1;
	if(!this.settings.show_194_min) this.settings.show_194_min = 20;
	if(!this.settings.show_194_minc) this.settings.show_194_minc = "#0000FF";
	if(!this.settings.show_194_max) this.settings.show_194_max = 60;
	if(!this.settings.show_194_maxc) this.settings.show_194_maxc = "#FF0000";
	if(!this.settings.show_9) this.settings.show_9 = 1;
	if(!this.settings.show_9_max) this.settings.show_9_max = 17520;
	if(!this.settings.show_4) this.settings.show_4 = 1;
	if(!this.settings.show_4_max) this.settings.show_4_max = 1095;

	//The Sonsore
	this.smart0 = new class_PercentBar(this.frame,0,mod_sysmonitor_size*0,"---");
	this.smart0.PercentColor(this.settings.color);
	this.smart0.PercentBarColor(this.settings.barcolor);

	if(this.settings.smartLabel=="") this.smart0.text_name.data = wmi.Get("Win32_DiskDrive.DeviceID='" + this.settings.smartID + "'").Properties_.Item("Caption").Value.substr(0,16);
	else this.smart0.text_name.data = this.settings.smartLabel;

	this.smart194 = new class_PercentBar(this.frame,0,mod_sysmonitor_size*1,getLocalizedString2("mod_sysm_plugin_smart_Label_194"));
	this.smart194.PercentColor(this.settings.color);
	this.smart194.PercentBarColor(this.settings.barcolor);

	this.smart9 = new class_PercentBar(this.frame,0,mod_sysmonitor_size*2,getLocalizedString2("mod_sysm_plugin_smart_Label_9"));
	this.smart9.PercentColor(this.settings.color);
	this.smart9.PercentBarColor(this.settings.barcolor);

	this.smart4 = new class_PercentBar(this.frame,0,mod_sysmonitor_size*3,getLocalizedString2("mod_sysm_plugin_smart_Label_4"));
	this.smart4.PercentColor(this.settings.color);
	this.smart4.PercentBarColor(this.settings.barcolor);


	// Update timer
	var obj = this;
	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_Update.call(obj); };
	plugin_Update.call(obj);


	// Calculating sensore height (required!)
	var OffsetI=1;
	this.frame.height = this.smart0.background.vOffset + this.smart0.background.height;
	if(this.settings.show_194==1) {
		this.smart194.vOffset = mod_sysmonitor_size*OffsetI++;
		this.frame.height = this.smart194.background.vOffset + this.smart194.background.height;
	} else
		this.smart194.vOffset = mod_sysmonitor_size*5;
	if(this.settings.show_9==1) {
		this.smart9.vOffset = mod_sysmonitor_size*OffsetI++;
		this.frame.height = this.smart9.background.vOffset + this.smart9.background.height;
	} else
		this.smart9.vOffset = mod_sysmonitor_size*5;
	if(this.settings.show_4==1) {
		this.smart4.vOffset = mod_sysmonitor_size*OffsetI++;
		this.frame.height = this.smart4.background.vOffset + this.smart4.background.height;
	} else
		this.smart4.vOffset = mod_sysmonitor_size*5;
		
	return true;


// --------------------------------------------------------------------------------
// Helper Functions


	function plugin_Edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_barcolor");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.barcolor;

		var smartDevices = new Array();
		var smartDevicesName = new Array();
		if(VBSEnabled) smartDevices = runCommand('cscript.exe //nologo "' + mod_sysmonitor_path_smart + '"').split("\n");
		else smartDevices = runCommand('"' + mod_sysmonitor_WMIExecQuery + '" "DeviceID" "1" "SELECT * FROM Win32_DiskDrive"').split("\n");
		for(var i = 0 ; i < smartDevices.length ; i++ ) {
			smartDevicesName[i] = wmi.Get("Win32_DiskDrive.DeviceID='" + smartDevices[i] + "'").Properties_.Item("Caption").Value;
		}

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_device");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = smartDevicesName;
		Drive_Edit_DialogFields[FieldID].optionValue = smartDevices;
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.smartID;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_Label");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.smartLabel;
		Drive_Edit_DialogFields[FieldID].description = getLocalizedString2("mod_sysm_plugin_smart_pref_LabelDesc");

		
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_mixed");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].option = Array(getLocalizedString2("mod_sysm_plugin_smart_pref_show_mixed_extended"),getLocalizedString2("mod_sysm_plugin_smart_pref_show_mixed_standard"),getLocalizedString2("mod_sysm_plugin_smart_pref_show_mixed_off"));
		Drive_Edit_DialogFields[FieldID].optionValue = Array(0,1,2);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_mixed;


		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_194");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_194;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_194_min");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_194_min;
		
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_194_minc");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_194_minc;
		
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_194_max");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_194_max;
		
		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_194_maxc");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_194_maxc;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_9");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_9;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_9_max");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_9_max;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_4");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_4;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_4_max");
		Drive_Edit_DialogFields[FieldID].type = 'text';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.show_4_max;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_smart_pref_show_Unit");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(0,1);
		Drive_Edit_DialogFields[FieldID].option = new Array("Celsius (°C)","Fahrenheit (F)");
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.showUnit;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(3,5,10,15,20,25,30,60,120,180,300);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;



		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.settings.barcolor = Drive_Edit_DialogResults[1];
			this.settings.smartID = Drive_Edit_DialogResults[2];
			this.settings.smartLabel = Drive_Edit_DialogResults[3];
			this.settings.showUnit = Drive_Edit_DialogResults[14];

			if(this.settings.smartLabel=="") this.smart0.text_name.data = wmi.Get("Win32_DiskDrive.DeviceID='" + this.settings.smartID + "'").Properties_.Item("Caption").Value.substr(0,16);
			else this.smart0.text_name.data = this.settings.smartLabel;


			var tmpReDraw = false;
			if( this.settings.show_194 != Drive_Edit_DialogResults[4] || this.settings.show_9 != Drive_Edit_DialogResults[5] || this.settings.show_4 != Drive_Edit_DialogResults[6] )
				tmpReDraw = true;

			this.settings.show_mixed = Drive_Edit_DialogResults[4];
			this.settings.show_194 = Drive_Edit_DialogResults[5];
			this.settings.show_194_min = Drive_Edit_DialogResults[6];
			this.settings.show_194_minc = Drive_Edit_DialogResults[7];
			this.settings.show_194_max = Drive_Edit_DialogResults[8];
			this.settings.show_194_maxc = Drive_Edit_DialogResults[9];
			this.settings.show_9 = Drive_Edit_DialogResults[10];
			this.settings.show_9_max = Drive_Edit_DialogResults[11];
			this.settings.show_4 = Drive_Edit_DialogResults[12];
			this.settings.show_4_max = Drive_Edit_DialogResults[13];


			this.smart0.PercentColor(this.settings.color);
			this.smart0.PercentBarColor(this.settings.barcolor);
			this.smart194.PercentColor(this.settings.color);
			this.smart194.PercentBarColor(this.settings.barcolor);
			this.smart9.PercentColor(this.settings.color);
			this.smart9.PercentBarColor(this.settings.barcolor);
			this.smart4.PercentColor(this.settings.color);
			this.smart4.PercentBarColor(this.settings.barcolor);

			if ( this.settings.updspeed != Drive_Edit_DialogResults[15] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[15];
				this.timer.interval = this.settings.updspeed;
			}

			plugin_Update.call(this);

			if(tmpReDraw) {
				var OffsetI=1;
				this.frame.height = this.smart0.background.vOffset + this.smart0.background.height;
				if(this.settings.show_194==1) {
					this.smart194.vOffset = mod_sysmonitor_size*OffsetI++;
					this.frame.height = this.smart194.background.vOffset + this.smart194.background.height;
				} else
					this.smart194.vOffset = mod_sysmonitor_size*5;
				if(this.settings.show_9==1) {
					this.smart9.vOffset = mod_sysmonitor_size*OffsetI++;
					this.frame.height = this.smart9.background.vOffset + this.smart9.background.height;
				} else
					this.smart9.vOffset = mod_sysmonitor_size*5;
				if(this.settings.show_4==1) {
					this.smart4.vOffset = mod_sysmonitor_size*OffsetI++;
					this.frame.height = this.smart4.background.vOffset + this.smart4.background.height;
				} else
					this.smart4.vOffset = mod_sysmonitor_size*5;
				mod_sysmonitor_ReDraw();
			}
		}
		delete Drive_Edit_DialogFields;
		return true;
	}


	function plugin_Update() {
		var tmpSensores = runCommand('"' + mod_sysmonitor_path_smartctl + '" -A sd' + String.fromCharCode( mod_sysmonitor_plugin_smart_refstring.charCodeAt(0) + parseInt(this.settings.smartID.substr(this.settings.smartID.length-1,1)) ));

/*
		print("  1: " + plugin_GetValue(tmpSensores,"  1"));
		print("  5: " + plugin_GetValue(tmpSensores,"  5"));
		print("  7: " + plugin_GetValue(tmpSensores,"  7"));
		print(" 10: " + plugin_GetValue(tmpSensores," 10"));
		print("195: " + plugin_GetValue(tmpSensores,"195"));
		print("196: " + plugin_GetValue(tmpSensores,"196"));
		print("197: " + plugin_GetValue(tmpSensores,"197"));
		print("198: " + plugin_GetValue(tmpSensores,"198"));
		print("201: " + plugin_GetValue(tmpSensores,"201"));
*/

		// Combination of all Error-Values (should be 0!)
/*
  1 Raw_Read_Error_Rate          => Releated with 195
  5 Reallocated_Sector_Ct
  7 Seek_Error_Rate              => Soft-Error
 10 Spin Retry Count
195 Hardware ECC Recovered       => Releated with 1
196 Reallocation Event Count
197 Current Pending Sector Count
198 Uncorrectable Sector Count
201 Soft Read Error Rate         => May be interesting
*/
		var tmp_0;
		if(this.settings.show_mixed == 0)
			tmp_0 = Math.abs(plugin_GetValue(tmpSensores,"  1") - plugin_GetValue(tmpSensores,"195")) + plugin_GetValue(tmpSensores,"  5") + plugin_GetValue(tmpSensores,"  7") + plugin_GetValue(tmpSensores," 10") + plugin_GetValue(tmpSensores,"196") + plugin_GetValue(tmpSensores,"197") + plugin_GetValue(tmpSensores,"198");
		if(this.settings.show_mixed == 1)
			tmp_0 = Math.abs(plugin_GetValue(tmpSensores,"  1") - plugin_GetValue(tmpSensores,"195")) + plugin_GetValue(tmpSensores,"  5") +  plugin_GetValue(tmpSensores," 10") + plugin_GetValue(tmpSensores,"196") + plugin_GetValue(tmpSensores,"198");

		if(this.settings.show_mixed != 2) {
			if(tmp_0>0) {
				this.smart0.PercentBarValue(tmp_0, getLocalizedString2("mod_sysm_plugin_smart_error"));
			} else {
				this.smart0.PercentBarValue(tmp_0, getLocalizedString2("mod_sysm_plugin_smart_ok"));
			}
		} else {
			this.smart0.PercentBarValue(0, "");
		}


		// Temperature_Celsius
		if(this.settings.show_194 == 1) {
			var tmp_194 = plugin_GetValue(tmpSensores,"194");
			if(tmp_194>0) {
				if(this.settings.showUnit == 1) {
					this.smart194.PercentBarValue( (tmp_194 - this.settings.show_194_min - 5) / (this.settings.show_194_max - this.settings.show_194_min + 10) * 100 , Math.round((212-32)/100 * parseInt(tmp_194) + 32) + " F" );
				} else {
					this.smart194.PercentBarValue( (tmp_194 - this.settings.show_194_min - 5) / (this.settings.show_194_max - this.settings.show_194_min + 10) * 100 , tmp_194 + " °C" );
				}
				if(tmp_194<this.settings.show_194_min)
					this.smart194.PercentBarColor(this.settings.show_194_minc);
				else if(tmp_194>this.settings.show_194_max)
					this.smart194.PercentBarColor(this.settings.show_194_maxc);
				else
					this.smart194.PercentBarColor(this.settings.barcolor);

			} else {
				this.smart194.PercentBarValue(100, getLocalizedString2("mod_sysm_plugin_offline"));
			}
		}


		// Power_On_Hours
		if(this.settings.show_9 == 1) {
			var tmp_9 = plugin_GetValue(tmpSensores,"  9");
			if(tmp_9>0) {
				this.smart9.PercentBarValue( (tmp_9 / this.settings.show_9_max * 100) , tmp_9 + " h" );
			} else {
				this.smart9.PercentBarValue(100, getLocalizedString2("mod_sysm_plugin_offline"));
			}
		}


		// Start Stop Count
		var tmp_4 = plugin_GetValue(tmpSensores,"  4");
		if(this.settings.show_4 == 1) {
			var tmp_4 = plugin_GetValue(tmpSensores,"  4");
			if(tmp_4>0) {
				this.smart4.PercentBarValue((tmp_4 / this.settings.show_4_max * 100),tmp_4 + " x");
			} else {
				this.smart4.PercentBarValue(100, getLocalizedString2("mod_sysm_plugin_offline"));
			}
		}


		// Spin Up Time
		//var tmp_3 = plugin_GetValue(tmpSensores,"  3");
		//print("Result 3: " + tmp_3);


		// Throuput Performance
		//var tmp_2 = plugin_GetValue(tmpSensores,"  2");
		//print("Result 2: " + tmp_2);


		return true;
	}

	function plugin_GetValue(src,key) {
		var i = 0;
		var tmp = "";
		var res = 0;

		i = src.indexOf("\n" + key + " ");
		if(i>0) {
			tmp = src.substring(i+1);
			i = tmp.indexOf("\n");
			//tmp = tmp.substring(0,i);
			//i = tmp.lastIndexOf(" ");
			//res = tmp.substring(i + 1);
			tmp = tmp.substring(87,i); //smartctl use fixed width!
			res = parseInt(tmp);
		} else {
			res = 0;
		}
		return parseInt(res);
	}


	function plugin_ReDraw() {
		this.frame.width    = mod_sysmonitor_width;
		this.smart0.style = preferences.mod_sysmonitor_bar_style.value;
		this.smart0.size  = mod_sysmonitor_size;
		this.smart194.style = preferences.mod_sysmonitor_bar_style.value;
		this.smart194.size  = mod_sysmonitor_size;
		this.smart9.style = preferences.mod_sysmonitor_bar_style.value;
		this.smart9.size  = mod_sysmonitor_size;
		this.smart4.style = preferences.mod_sysmonitor_bar_style.value;
		this.smart4.size  = mod_sysmonitor_size;
		var OffsetI=1;
		this.frame.height = this.smart0.background.vOffset + this.smart0.background.height;
		if(this.settings.show_194==1) {
			this.smart194.vOffset = mod_sysmonitor_size*OffsetI++;
			this.frame.height = this.smart194.background.vOffset + this.smart194.background.height;
		} else
			this.smart194.vOffset = mod_sysmonitor_size*5;
		if(this.settings.show_9==1) {
			this.smart9.vOffset = mod_sysmonitor_size*OffsetI++;
			this.frame.height = this.smart9.background.vOffset + this.smart9.background.height;
		} else
			this.smart9.vOffset = mod_sysmonitor_size*5;
		if(this.settings.show_4==1) {
			this.smart4.vOffset = mod_sysmonitor_size*OffsetI++;
			this.frame.height = this.smart4.background.vOffset + this.smart4.background.height;
		} else
			this.smart4.vOffset = mod_sysmonitor_size*5;
		return true;
	}


}

// --------------------------------------------------------------------------------
// The END